﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcShop.Extensions.Encryption;

namespace MvcShop.Models.Services
{
    public interface IAmountService
    {
        bool UserRecharge(string giftCardNo, string giftCardKey,string userName);
        bool OrdersPay(string billNo, string userName);
    }

    public class AmountService : ServiceBase, IAmountService
    {
        #region IAmountService 成员
        public bool UserRecharge(string giftCardNo, string giftCardKey,string userName)
        {
            bool has = false;
            System.Data.Common.DbTransaction tran = null;
            try
            {
                this.dataContext.Connection.Open();
                tran = this.dataContext.Connection.BeginTransaction();

                var query = (from gc in this.dataContext.GiftCards
                             where gc.GiftCardNo == giftCardNo && gc.GiftCardKey == giftCardKey && gc.IsUsed == false
                             select gc).FirstOrDefault();
                has = (query != null);
                if (has)
                {
                    SymmetricMethod smd=new SymmetricMethod();
                    query.IsUsed = true;
                    query.UserName = userName;
                    query.UsedTime = DateTime.Now;

                    var uAmount = (from ua in this.dataContext.UserAmounts
                                   where ua.UserName == userName
                                   select ua).FirstOrDefault();

                    if (uAmount != null)
                    {
                        decimal me = Convert.ToDecimal(smd.Decrypto(uAmount.Amount));
                        uAmount.Amount = smd.Encrypto((me + query.GiftCardValue).ToString());
                    }
                    else
                    {
                        var uAm = new UserAmount();
                        uAm.UserName = userName;
                        uAm.Amount = smd.Encrypto(query.GiftCardValue.ToString());
                        this.dataContext.AddToUserAmounts(uAm);
                    }
                    this.dataContext.SaveChanges();
                    tran.Commit();
                }
            }
            catch (Exception ex)
            {
                if (tran != null)
                    tran.Rollback();
                return false;
                throw ex;
            }
            finally
            {
                if (this.dataContext != null && this.dataContext.Connection.State != System.Data.ConnectionState.Closed)
                    this.dataContext.Connection.Close();

            }
            return has;
        }

        public bool OrdersPay(string billNo, string userName)
        {
            bool has = false;
            System.Data.Common.DbTransaction tran = null;
            try
            {
                this.dataContext.Connection.Open();
                tran = this.dataContext.Connection.BeginTransaction();

                var query = (from ua in this.dataContext.UserAmounts
                             where ua.UserName==userName
                             select ua).FirstOrDefault();
                has = (query != null);
                if (has)
                {
                    decimal aTotal = 0;
                    SymmetricMethod smd = new SymmetricMethod();

                    var orders = (from od in this.dataContext.Orderses
                                  where od.UserName == userName && od.BillNo == billNo
                                  select od).FirstOrDefault();

                    if (orders != null)
                    {
                        aTotal = Convert.ToDecimal(smd.Decrypto(query.Amount));
                        decimal overage = aTotal - orders.TotalValue;

                        if (overage > 0)
                        {
                            query.Amount = smd.Encrypto(overage.ToString());

                            orders.IsPayment = true;
                            this.dataContext.SaveChanges();
                            tran.Commit();
                        }
                        else
                        {
                            has = false;
                        }
                    }
                    else
                    {
                        has = false;
                    }
                }            
            }
            catch (Exception ex)
            {
                if (tran != null)
                    tran.Rollback();
                return false;
                throw ex;
            }
            finally
            {
                if (this.dataContext != null && this.dataContext.Connection.State != System.Data.ConnectionState.Closed)
                    this.dataContext.Connection.Close();

            }
            return has;
        }
        #endregion
    }
}
